package com.jstyle.jstylecommon.scan;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jstyle.jstylecommon.R;
import com.jstyle.jstylecommon.R2;
import com.jstyle.jstylecommon.model.ExtendedBluetoothDevice;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LeDeviceListAdapter extends RecyclerView.Adapter {


    private List<ExtendedBluetoothDevice> deviceList;
    int filterRssi;

    public void setDeviceList(List<ExtendedBluetoothDevice> deviceList) {
        this.deviceList = deviceList;
        notifyDataSetChanged();
    }

    public void setFilterRssi(int rssi) {
        this.filterRssi = rssi;
    }

    public LeDeviceListAdapter() {
        super();
        deviceList = new ArrayList<>();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_device, parent, false);
        return new DeviceViewHolder(view);
    }

    private static final String TAG = "LeDeviceListAdapter";
    onItemClickListener onItemClickListener;

    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            DeviceViewHolder viewHolder = (DeviceViewHolder) holder;
            ExtendedBluetoothDevice extendedBluetoothDevice = deviceList.get(position);
            if (extendedBluetoothDevice.rssi == ExtendedBluetoothDevice.NO_RSSI) {
                viewHolder.deviceRssi.setText("Bonded");
            } else {
                viewHolder.deviceRssi.setText("Rssi:" + extendedBluetoothDevice.rssi);
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        DeviceViewHolder viewHolder = (DeviceViewHolder) holder;
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(holder.getAdapterPosition());
            }
        });
        Context context=viewHolder.itemView.getContext();
        ExtendedBluetoothDevice extendedBluetoothDevice = deviceList.get(position);
        BluetoothDevice device = extendedBluetoothDevice.device;
        final String deviceName = extendedBluetoothDevice.name;
        viewHolder.deviceName.setText(TextUtils.isEmpty(deviceName)?
                context.getResources().getString(R.string.unknown_device):deviceName);
        viewHolder.deviceAddress.setText(device.getAddress());
        if (extendedBluetoothDevice.rssi == ExtendedBluetoothDevice.NO_RSSI) {
            viewHolder.deviceRssi.setText("Bonded");
        } else {
            viewHolder.deviceRssi.setText("Rssi:" + extendedBluetoothDevice.rssi);
        }
    }

    class DeviceViewHolder extends RecyclerView.ViewHolder {

        @BindView(R2.id.deviceName)
        TextView deviceName;
        @BindView(R2.id.deviceAddress)
        TextView deviceAddress;
        @BindView(R2.id.deviceRssi)
        TextView deviceRssi;


        public DeviceViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void addBondDevice(List<ExtendedBluetoothDevice> list) {

        deviceList.addAll(list);
        notifyDataSetChanged();

    }

    public void addDevice(BluetoothDevice device, String name, int rssi) {
        ExtendedBluetoothDevice bluetoothDevice = findDevice(device);
        if (bluetoothDevice == null) {
            int size = deviceList.size();
            deviceList.add(new ExtendedBluetoothDevice(device, name, rssi));
            notifyItemInserted(size);
        } else {
            bluetoothDevice.rssi = rssi;
            int index = deviceList.indexOf(bluetoothDevice);
            notifyItemChanged(index, 1);
        }

    }

    private ExtendedBluetoothDevice findDevice(final BluetoothDevice device) {
        for (final ExtendedBluetoothDevice mDevice : deviceList) {
            if (mDevice.matches(device)) return mDevice;
        }
        return null;
    }

    public BluetoothDevice getDevice(int position) {
        return deviceList.get(position).device;
    }

    public String getName(int position) {
        return deviceList.get(position).name;
    }

    public void clear() {
        deviceList.clear();
        notifyDataSetChanged();
    }


    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return deviceList == null ? 0 : deviceList.size();
    }

    boolean isClick;

    public interface onItemClickListener {
        public void onItemClick(int position);
    }


}

